package gui.custom;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JPanel;

import backend.rss.items.SwatchItem;
import backend.rss.items.ThemeItem;

public abstract class AbstractSwatchPanel extends JPanel implements MouseListener
{
	private JPanel swatch1;
	private JPanel swatch2;
	private JPanel swatch3;
	private JPanel swatch4;
	private JPanel swatch5;
	
	private ThemeItem item;
	
	public AbstractSwatchPanel(ThemeItem item, Dimension swatchSize)
	{
		super(new FlowLayout(FlowLayout.LEFT, 0, 0));
		setPreferredSize(swatchSize);
		setSize(swatchSize);
		
		this.item = item;
		
		List<SwatchItem> swatches = item.getSwatches();
		
		Dimension aSwatchSize = new Dimension(swatchSize.width/5, swatchSize.height);
		
		swatch1	= createASwatch(swatches.get(0), aSwatchSize);
		swatch2	= createASwatch(swatches.get(1), aSwatchSize);
		swatch3	= createASwatch(swatches.get(2), aSwatchSize);
		swatch4	= createASwatch(swatches.get(3), aSwatchSize);
		swatch5	= createASwatch(swatches.get(4), aSwatchSize);
		
		add(swatch1);
		add(swatch2);
		add(swatch3);
		add(swatch4);
		add(swatch5);

		swatch1.addMouseListener(this);
		swatch2.addMouseListener(this);
		swatch3.addMouseListener(this);
		swatch4.addMouseListener(this);
		swatch5.addMouseListener(this);
	}
	
	protected ThemeItem getThemeItem()
	{
		return item;
	}
	
	private JPanel createASwatch(SwatchItem swatch, Dimension aSwatchSize)
	{
		JPanel swatchPanel = new JPanel();
		swatchPanel.setPreferredSize(aSwatchSize);
		swatchPanel.setSize(aSwatchSize);

		Color swatchColor;
		
		if (swatch.getHex() != null)
		{
			swatchColor = new Color(Integer.parseInt(swatch.getHex(), 16));
		}
		else
		if (swatch.getMode().equals("rgb"))
		{
			swatchColor = new Color(swatch.getC1(), swatch.getC2(), swatch.getC3());
		}
		else
		{	
			float red 	= 1 - swatch.getC1() + swatch.getC4() * (swatch.getC1()-1);
			float green = 1 - swatch.getC2() + swatch.getC4() * (swatch.getC2()-1);
			float blue	= 1 - swatch.getC3() + swatch.getC4() * (swatch.getC3()-1);
			
			swatchColor	= new Color(red, green, blue);
		}
		swatchPanel.setBackground(swatchColor);
		
		return swatchPanel;
	}
	
	protected JPanel getSwatch1()
	{
		return swatch1;
	}

	protected JPanel getSwatch2()
	{
		return swatch2;
	}

	protected JPanel getSwatch3()
	{
		return swatch3;
	}

	protected JPanel getSwatch4()
	{
		return swatch4;
	}

	protected JPanel getSwatch5()
	{
		return swatch5;
	}
	
	public void mouseClicked(MouseEvent arg0)
	{
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent arg0)
	{
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));	
	}

	public void mouseExited(MouseEvent arg0)
	{
		setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));	
	}

	public void mousePressed(MouseEvent arg0)
	{
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0)
	{
		// TODO Auto-generated method stub
		
	}
}
